#include "stdafx.h"
//-------------------------------------------------------------------------------------------------
//ImageForm.cpp : GUI Image Form  (dialog like)  - displays the image and its properties (read only)     
//-------------------------------------------------------------------------------------------------
//FreeBSD License
//Copyright 2011 Will Stott All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are
//permitted provided that the following conditions are met:
//
//   1. Redistributions of source code must retain the above copyright notice, this list of
//      conditions and the following disclaimer.
//
//   2. Redistributions in binary form must reproduce the above copyright notice, this list
//      of conditions and the following disclaimer in the documentation and/or other materials
//      provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY Will Stott ``AS IS'' AND ANY EXPRESS OR IMPLIED
//WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
//FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Will Stott OR
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
//ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//The views and conclusions contained in the software and documentation are those of the
//authors and should not be interpreted as representing official policies, either expressed
//or implied, of Will Stott.
//
//----Authors--------------------------------------------------------------------------------------
//created by: will.stott@ucl.ac.uk Oct 2011
//
//----Comments-------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------

#include "ArchiveProcGUI.h"
#include "ArchiveProcGUIDoc.h"
#include "MainFrm.h"
#include "ImageForm.h"


// CImageForm

IMPLEMENT_DYNCREATE(CImageForm, CFormView)

CImageForm::CImageForm()
	: CFormView(CImageForm::IDD),
	m_Changed(false),
	m_InitialUpdateDone(FALSE),
	m_SelectedImageDicomRef(_T("."))
{
	//!!!!!!!!!!!!!!!
#ifdef _DEBUG
	MxUtils::OutputDebugMsg("CImageForm::CImageForm()", "image form ctor");
#endif
	//!!!!!!!!!!!!!!!

	//  m_Notes = _T("");
	m_SelectedToolDesc = _T("");
	m_SelImageObjectDesc = _T("");
	m_ImageType = _T("");
}

CImageForm::~CImageForm()
{
//!!!!!!!!!!!!!!!
#ifdef _DEBUG
	MxUtils::OutputDebugMsg("CImageForm::~CImageForm()", "image form dtor");
#endif
//!!!!!!!!!!!!!!!
}


void CImageForm::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_PATHFILENAME, m_SelectedImageDicomRef);
	DDX_Control(pDX, IDC_IMAGE, m_Image);
	DDX_Text(pDX, IDC_TOOLSELECTION, m_SelectedToolDesc);
	DDX_Text(pDX, IDC_SELIMAGEOBJECT, m_SelImageObjectDesc);
	DDX_Text(pDX, IDC_IMAGETYPE, m_ImageType);
}

BEGIN_MESSAGE_MAP(CImageForm, CFormView)
	ON_WM_CREATE()
END_MESSAGE_MAP()


// CImageForm diagnostics

#ifdef _DEBUG
void CImageForm::AssertValid() const
{
	CFormView::AssertValid();
}

#ifndef _WIN32_WCE
void CImageForm::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}
#endif
#endif //_DEBUG


// CImageForm message handlers

int CImageForm::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	int rc = -1;
//!!!!!!!!!!!!!!!
#ifdef _DEBUG
	MxUtils::OutputDebugMsg("CImageForm::OnCreate()", "create image form");
#endif
//!!!!!!!!!!!!!!!

	if (CFormView::OnCreate(lpCreateStruct) == -1)
		CArchiveProcGUIApp::UserErrorMsg("Fatal Error", "unable to create image form", "unable to recover", "restart application", MB_ICONSTOP | MB_OK );
	else
	{
		m_Image.SubclassDlgItem(IDC_IMAGE, this);
		rc = 0;
	}
	return rc;
}



void CImageForm::OnInitialUpdate()
{
//!!!!!!!!!!!!!!!
#ifdef _DEBUG
	MxUtils::OutputDebugMsg("CImageForm::OnInitialUpdate()", "first update of image form after (File | New) or (File | Open)");
#endif
//!!!!!!!!!!!!!!!

	CFormView::OnInitialUpdate();

	CArchiveProcGUIDoc *pDoc = dynamic_cast<CArchiveProcGUIDoc *>(GetDocument()); 
	if (pDoc != '\0' )
	{
		m_InitialUpdateDone = TRUE;

		if (pDoc->IsIDFDocumentLoaded())
			pDoc->UpdateAllViews(this, CArchiveProcGUIDoc::UpdateAllViewsOpenDoc);			//Open Document caused CImageForm::OnInitialUpdate()
		else
			pDoc->UpdateAllViews(this, CArchiveProcGUIDoc::UpdateAllViewsSetNewImage);		//New Document caused CImageForm::OnInitialUpdate() - views will discover that there is no image

		pDoc->SetModifiedFlag(FALSE);
	}
}

void CImageForm::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
	CArchiveProcGUIDoc  *pDoc = dynamic_cast<CArchiveProcGUIDoc *>(GetDocument()); 
	ASSERT(pDoc != '\0');

//!!!!!!!!!!!!!!!
#ifdef _DEBUG
	MxUtils::OutputDebugMsg("CImageForm::OnUpdate()", "(pSender=%s, lHint=%s, pHint%s) m_InitialUpdateDone=%d, selected image dicom [%s]",CArchiveProcGUIApp::GetRunTimeTypeName(pSender), CArchiveProcGUIDoc::GetUpdateHintStr(lHint), (pHint == '\0') ? "==NULL" : "!=NULL",  m_InitialUpdateDone, (pDoc->GetSelImageDicomRef() != '\0') ? pDoc->GetSelImageDicomRef() : "(invalid)" );
#endif
//!!!!!!!!!!!!!!!

	if ( m_InitialUpdateDone )
	{
		if (lHint == CArchiveProcGUIDoc::UpdateAllViewsSetBrightnessContrast)
		{
			m_Image.Invalidate();
			m_Image.RedrawWindow();
		}
		else if (lHint == CArchiveProcGUIDoc::UpdateAllViewsSetNewImage) 
		{
			if ( SetupFormDataForSelectedImage(pDoc) == false)
				m_Image.ShowWindow(SW_HIDE);
			else				
			{
				ImageDataset *pImageDataset = '\0';
				if ( (pImageDataset = pDoc->GetImageDatasetFile()) != '\0')
				{
					CString selectedImagePathFileName(pImageDataset->GetSelImagePathFileName());
					if (m_Image.LoadFromFile(selectedImagePathFileName ))
						m_Image.ShowWindow(SW_SHOWNORMAL);
				}
			}
		}
		else if (lHint ==  CArchiveProcGUIDoc::UpdateAllViewsSetNewImageType )
		{
			if (SetupFormDataForSelectedImage(pDoc) == false)
				m_Image.ShowWindow(SW_HIDE);
			else
				m_Image.RedrawWindow();
		}
		else if (lHint ==  CArchiveProcGUIDoc::UpdateAllViewsSetNewImageObject)
		{
			if (SetupFormDataForSelectedImage(pDoc) == false)
				m_Image.ShowWindow(SW_HIDE);
			else
				m_Image.RedrawWindow();
		}
		else if (lHint ==  CArchiveProcGUIDoc::UpdateAllViewsDeleteImageObject)		
		{
			if (SetupFormDataForSelectedImage(pDoc) == false)
				m_Image.ShowWindow(SW_HIDE);
			else
				m_Image.RedrawWindow();
		}
		else if (lHint == CArchiveProcGUIDoc::UpdateAllViewsSetNewTool)
		{
			if (SetupFormDataForSelectedImage(pDoc) == false)
				m_Image.ShowWindow(SW_HIDE);
			else
				m_Image.RedrawWindow();
		}
		else if (lHint == CArchiveProcGUIDoc::UpdateAllViewsChangeImageObjectDisplay)
		{
			if (SetupFormDataForSelectedImage(pDoc) == false)
				m_Image.ShowWindow(SW_HIDE);
			else
				m_Image.RedrawWindow();
		}
	}
	return;
}



//void CImageForm::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView)
//{
//	CArchiveProcGUIDoc *pDoc = '\0';
//	bool ChangeDoc = false;
//	CMainFrame *pMainFrame = dynamic_cast<CMainFrame *>(AfxGetMainWnd());
//	if ( (pMainFrame != '\0') && (pMainFrame->IsKindOf(RUNTIME_CLASS(CMainFrame))))
//	{
//		pDoc = CArchiveProcGUIDoc::GetActiveDoc();
//		if (pMainFrame->IsSelectedDoc(pDoc) == false)
//		{
//			ChangeDoc = true;
//			pMainFrame->SetSelectedDoc(pDoc);
//		}
//	}
////!!!!!!!!!!!!!!!
//#ifdef _DEBUG
//	MxUtils::OutputDebugMsg("CImageForm::OnActivateView()", "change doc=%d: view activate=%d,(pActivateView == pDeactiveView)=%d", ChangeDoc, bActivate,  (pActivateView == pDeactiveView) ? 1 : 0 );
//#endif
////!!!!!!!!!!!!!!!
//	CFormView::OnActivateView(bActivate, pActivateView, pDeactiveView);
//
//	if (pDoc != '\0')
//		pDoc->UpdateAllViews(this, CArchiveProcGUIDoc::UpdateAllViewsSetNewImage);	
//
//}


//-------------------------------------------------------------------------------------------------
//			private methods
//-------------------------------------------------------------------------------------------------


bool	CImageForm::SetupFormDataForSelectedImage(CArchiveProcGUIDoc  *pDoc)
{
	bool rc = false;

	m_SelectedToolDesc = L"(ERROR: pDoc is null)";		//should never appear
	m_SelImageObjectDesc = L"(ERROR: pDoc is null)";
	m_ImageType = L"(ERROR: pDoc is null)";
	m_SelectedImageDicomRef = L"(ERROR: pDoc is null)";

	if ( pDoc != '\0' )
	{
		ImageObjectTypeTool *pSelTool = pDoc->GetSelImageObjectTypeTool();
		ImageProps  *pSelectedImageProps = pDoc->GetSelImageProperties();

		if (pSelTool == '\0')
			m_SelectedToolDesc = L".";
		else
		{
			CString menuSel(pSelTool->GetImageObjectType()->GetMenuName());
			m_SelectedToolDesc.Format(L"Tools | Drawing -> %s", menuSel);
		}

		if (pSelectedImageProps == '\0')
		{
			m_ImageType= L"(ERROR: no props for selected image)";
			m_SelectedImageDicomRef= L"(ERROR: no props for selected image)";
			m_SelImageObjectDesc = L"(ERROR: no props for selected image)";
		}
		else
		{
			if (pSelectedImageProps->IsValidSelectedImage())
			{
				m_ImageType = (pSelectedImageProps->GetSelImageType() != '\0') ? pSelectedImageProps->GetSelImageType()->GetName() : "(no image type set)";
				m_SelectedImageDicomRef = pSelectedImageProps->GetDicomRef();
			}
			else
			{
				m_ImageType = L"..";
				m_SelectedImageDicomRef = L"...";
			}

			ImageObject *pSelImageObj = pSelectedImageProps->GetSelImageObject();
			if (pSelImageObj == '\0')
				m_SelImageObjectDesc.Format(L"Total Image Objects (%d), Show(%d) none selected", pSelectedImageProps->GetImageObjectCnt(), (pDoc->IsShowImageObjects() == true) ? 1 : 0);
			else
			{
				CString objSelDesc(pSelImageObj->GetDisplayName());
				CString objSelNote(pSelImageObj->GetNote());
				m_SelImageObjectDesc.Format(L"Total Image Objects (%d), Show(%d) selected: %s (%s)", pSelectedImageProps->GetImageObjectCnt(), (pDoc->IsShowImageObjects() == true) ? 1 : 0, objSelDesc, objSelNote);
			}
		}

		UpdateData(FALSE);

		rc = ( (pDoc->IsIDFDocumentLoaded()) && (pSelectedImageProps->IsValidSelectedImage()) ) ? true : false;

	}
	return rc;
}

//-------------------------------------------------------------------------------------------------
//			E N D   O F   F I L E
//-------------------------------------------------------------------------------------------------











